package com.microblogforsim.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.microblogforsim.R;
import com.microblogforsim.adapter.FragmentAdapter;
import com.microblogforsim.adapter.MyGridViewAdapter;
import com.microblogforsim.fragment.Fragment_Comment;
import com.microblogforsim.model.Topics;
import com.microblogforsim.ui.PictureGridView;
import com.microblogforsim.utils.ACache;

import java.util.ArrayList;
import java.util.List;

public class Topics_detailActivity extends AppCompatActivity {

    private TextView tv_content;
    private TextView tv_name;
    private TextView tv_time;
    private TextView tv_from;
    private ImageView ig_src;
    private int pos = 0;
    private PictureGridView gridview;

    private Topics.DataBean detail = new Topics.DataBean();
    private ACache maCache;

    private XTabLayout tabLayout;
    private ViewPager tab_content;
    private List<Fragment> fragments;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topics_detail);
        maCache = ACache.get(Topics_detailActivity.this);
        //初始化组件
        initView();
        //获取页面传参
        getIntentData();
        //设置组件值
        initInfo();
//        设置评论导航
        initViewPage();

    }

    private void initView() {

        tv_content = findViewById(R.id.topic_detail_content);
        tv_from = findViewById(R.id.topic_detail_from);
        tv_time = findViewById(R.id.topic_detail_time);
        tv_name = findViewById(R.id.topic_detail_uname);
        ig_src = findViewById(R.id.topic_detail_upic);
        gridview = findViewById(R.id.topic_list_gridView);
        tabLayout = findViewById(R.id.topics_detail_tab_tab);
        tab_content = findViewById(R.id.topics_detail_tab_content);

    }

    private void initInfo() {
        //获取当前的图片数目
        int num = 0;
        if (detail.getImg().size() > 0) {

            num = pos % detail.getImg().size() + 1;
        } else {
            num = 0;
        }
        int col = 1;//默认列数
        Log.i("tag", "num" + num);
        if (num == 1) {
            gridview.setNumColumns(1);
            col = 1;
        } else if (num == 2 || num == 4) {
            gridview.setNumColumns(2);
            col = 2;
        } else {
            gridview.setNumColumns(3);
            col = 3;
        }

        gridview.setAdapter(new MyGridViewAdapter(Topics_detailActivity.this, num, col, detail.getImg()));

        tv_content.setText(detail.getContent() + "");
        tv_name.setText(detail.getUser().get(0).getUserName());
        tv_time.setText(detail.getT());
        Glide.with(Topics_detailActivity.this)
                //加载地址this.imglist.get(position).getSrc()+
                .load(detail.getUser()
                        .get(0).getImg() + "")
                //加载失败时，设置默认的图片
                .placeholder(R.drawable.loading)
                //显示缩略图
                .thumbnail(0.2f)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                //显示的位置
                .into(ig_src);


    }

    private void getIntentData() {

        Intent intent = getIntent();
        pos = intent.getIntExtra("pos", 0);
        detail = (Topics.DataBean) maCache.getAsObject("topic");

    }

    private void initViewPage(){
        fragments = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        titles.add("转发");
        titles.add("评论");
        fragments.add(new Fragment_Comment());
        fragments.add(new Fragment_Comment());
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(),fragments,titles);
        tab_content.setAdapter(adapter);
        tab_content.setOffscreenPageLimit(2);
        tabLayout.setupWithViewPager(tab_content);
    }

}